package de.beuth.hochschule.ats.activitys;

import java.sql.SQLException;
import java.util.List;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.j256.ormlite.android.apptools.OrmLiteBaseListActivity;
import com.j256.ormlite.dao.Dao;

import de.beuth.hochschule.ats.R;
import de.beuth.hochschule.ats.entitys.DatabaseHelper;
import de.beuth.hochschule.ats.entitys.Lecture;
import de.beuth.hochschule.ats.entitys.Vocable;

public class ShowLecture extends OrmLiteBaseListActivity<DatabaseHelper>
		implements OnClickListener {

	private int lecture_id;
	private AlertDialog.Builder builder;
	private AlertDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lecture);
		if (savedInstanceState == null) {
			lecture_id = getIntent().getExtras().getInt("lecture_id");
		} else {
			lecture_id = savedInstanceState.getInt("lecture_id");
		}

		builder = new AlertDialog.Builder(this);

		findViewById(R.id.save_button).setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		Lecture lecture;
		List<Vocable> cards;
		try {
			lecture = getHelper().getLectureDao().queryForId(lecture_id);
			setTitle(lecture.getName());
			cards = getHelper().getVocableDao().queryForEq("lecture_id",
					lecture.getId());
			setListAdapter(new ArrayAdapter<Vocable>(this, R.layout.row, cards));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Vocable vocable = (Vocable) l.getItemAtPosition(position);
		Intent i = new Intent(this, EditVocable.class);
		i.setAction(Intent.ACTION_EDIT);
		i.putExtra("vocable_id", vocable.getId());
		
		startActivity(i);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate menu from XML resource
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.lecture_menu, menu);

		// Generate any additional actions that can be performed on the
		// overall list. In a normal install, there are no additional
		// actions found here, but this allows other applications to extend
		// our menu with their own actions.
		Intent intent = new Intent(null, getIntent().getData());
		intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
		menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0,
				new ComponentName(this, ShowLecture.class), null, intent, 0,
				null);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		Intent intent;

		switch (item.getItemId()) {
		case R.id.add:
			intent = new Intent(this, EditVocable.class);
			intent.setAction(Intent.ACTION_INSERT);
			intent.putExtra("lecture_id", lecture_id);
			startActivity(intent);
			return true;
		case R.id.edit:
			intent = new Intent(this, EditLecture.class);
			intent.setAction(Intent.ACTION_EDIT);
			intent.putExtra("lecture_id", lecture_id);
			startActivity(intent);
			return true;
		case R.id.delete:
			showLectureDeleteConfirmationDialog();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void onClick(View v) {
		Intent intent = new Intent(this, EditVocable.class);
		intent.setAction(Intent.ACTION_INSERT);
		intent.putExtra("lecture_id", lecture_id);
		startActivity(intent);
	}

	private void showLectureDeleteConfirmationDialog() {
		builder.setMessage(R.string.delete_lecture_confirmation);
		builder.setTitle(R.string.delete_lecture);
		builder.setCancelable(true);
		builder.setPositiveButton(R.string.yes,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						Dao<Lecture, Integer> lectureDao;
						Dao<Vocable, Integer> vocableDao;
						try {
							lectureDao = getHelper().getLectureDao();
							vocableDao = getHelper().getVocableDao();
							vocableDao.delete(vocableDao.queryForEq(
									"lecture_id", lecture_id));
							lectureDao.delete(lectureDao.queryForId(lecture_id));
							finish();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						dialog.cancel();
					}
				});
		builder.setNegativeButton(R.string.no,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
					}
				});
		dialog = builder.create();
		dialog.show();
	}

}
